package com.runshu.newsmartark.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.runshu.newsmartark.activity.MainActivity;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.util.common.LogUtil;

/**
 * Demo class
 *
 * @author yu
 * @date 12/23/20
 * @decs：
 */

public class UpdateRestartReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        LogUtil.i("Logger", "onReceive: "+intent.getAction());
        String packageName = intent.getDataString();
        if (intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) {//接收自己app升级广播
            Intent intent2 = new Intent(context, MainActivity.class);//重启app
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
            LogUtil.i("Logger",   "onReceive:重启app" + packageName);
        }else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
            Intent intent2 = new Intent(context, MainActivity.class);//重启app
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
            LogUtil.i("Logger",   "onReceive:重启app" + packageName);
        }

        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {//接收安装广播

            LogUtil.i("Logger",  "onReceive:安装了" + packageName);
            if (packageName.equals("package:" + MyApplication.getApplication().getPackageName())) {
                /*SystemUtil.reBootDevice();*/
            }
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) { //接收卸载广播
            LogUtil.i("Logger",   "onReceive:卸载了" + packageName);
        }

    }
}